import 'package:flutter/material.dart';

class FormDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Form'),),
      body: Theme(
        // data: ThemeData(
        data: Theme.of(context).copyWith(
          primaryColor: Colors.black,
        ),
        child: Container(
          padding: EdgeInsets.all(16),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [TextFieldDemo(), RegisterForm()],

          ),
        ),
      ),
    );
  }
}

class RegisterForm extends StatefulWidget {
  @override
  State<RegisterForm> createState() => _RegisterFormState();
}

class _RegisterFormState extends State<RegisterForm> {
  final registerFormKey = GlobalKey<FormState>();
  late String user, pass;
  bool autovalidate = false;

  void submit() {
    if (registerFormKey.currentState!.validate()) {
      registerFormKey.currentState?.save();
      debugPrint(user + pass);



      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(
          content: Text('This is a snackbar'),
          duration: Duration(seconds: 3),
        ),
      );
    } else {
      setState(() {
        autovalidate = true;
      });
    }
  }

  String? validateUser(value) {
    if (value.toString().isEmpty) {
      return 'User is required';
    }
    return null;
  }

  String? validatePass(value) {
    if (value.toString().isEmpty) {
      return 'Pass is required';
    }
    return null;
  }

  @override
  Widget build(BuildContext context) {
    return Form(
        key: registerFormKey,
        child: Column(
          children: [
            TextFormField(
              decoration: InputDecoration(
                labelText: 'user',
                helperText: '',
              ),
              onSaved: (value) {
                user = value!;
              },
              validator: validateUser,
              autovalidateMode: AutovalidateMode.onUserInteraction,
            ),
            TextFormField(
              obscureText: true,
              decoration: InputDecoration(
                labelText: 'pass',
                helperText: '',
              ),
              onSaved: (value) {
                pass = value!;
              },
              validator: validatePass,
            ),
            SizedBox(
              height: 16,
            ),
            Container(
              width: double.infinity,
              child: ElevatedButton(
                onPressed: submit,
                child: Text('register'),
                style: ButtonStyle(
                  elevation: MaterialStateProperty.all(30),
                  backgroundColor: MaterialStateProperty.resolveWith(
                    (Set<MaterialState> states) {
                      if (states.contains(MaterialState.pressed)) {
                        return Colors.black;
                      }
                      ;
                      return Colors.red;
                    },
                  ),
                ),
              ),
            )
          ],
        ));
  }
}

class TextFieldDemo extends StatefulWidget {
  @override
  State<TextFieldDemo> createState() => _TextFieldDemoState();
}

class _TextFieldDemoState extends State<TextFieldDemo> {
  final textEditingController = TextEditingController();

  @override
  void dispose() {
    // TODO: implement dispose
    textEditingController.dispose();
    super.dispose();
  }

  @override
  void initState() {
    // TODO: implement initState
    // textEditingController.text = 'hi';
    textEditingController.addListener(() {
      debugPrint('Listener:' + textEditingController.text);
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return TextField(
      controller: textEditingController,
      onChanged: (value) {
        debugPrint(value);
      },
      onSubmitted: (value) {
        debugPrint('submit:' + value);
      },
      decoration: InputDecoration(
        icon: Icon(
          Icons.subject,
          color: Colors.yellow,
        ),
        labelText: 'title',
        hintText: 'enter',
        // border: InputBorder.none,
        // border: OutlineInputBorder(),
        filled: true,
        fillColor: Colors.grey,
      ),
    );
  }
}

class ThemeDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      color: Theme.of(context).hintColor,
    );
  }
}
